﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.IO;
using System.Net.Mime;
using Dss.Data;

namespace Dss.Methods
{
    //Email class contains all the methods used to access the database from the Emailwindow
    public class Email
    {
        public Email()
        {
            cecbContext = new DSSEntities();
        }
        DSSEntities cecbContext;

        // Email is generated and sent       
        public bool genarateEmail(String from, String to, String cc, String displayName, String password, String subjet, String body)
        {
            bool EmailIsSent = false;

            MailMessage m = new MailMessage();
            SmtpClient sc = new SmtpClient();
            try
            {
                m.From = new MailAddress(from, displayName);
                m.To.Add(new MailAddress(to, displayName));
                // m.CC.Add(new MailAddress("sliititb1@gmail.com", "Display name CC"));

                m.Subject = subjet;
                m.IsBodyHtml = true;
                m.Body = body;



                //FileStream fs = new FileStream("D:\\devided.txt",
                //                   FileMode.Open, FileAccess.Read);
                //Attachment a = new Attachment(fs, "devided.txt",
                //                   MediaTypeNames.Application.Octet);
                //m.Attachments.Add(a);

                //string str = "<html><body><h1>Picture</h1><br/><img src=\"cid:image1\"></body></html>";
                //AlternateView av =
                //             AlternateView.CreateAlternateViewFromString(str,
                //             null,MediaTypeNames.Text.Html);
                //LinkedResource lr = new LinkedResource("D:\\x.jpg",
                //             MediaTypeNames.Image.Jpeg);
                //lr.ContentId = "image1";
                //av.LinkedResources.Add(lr);
                //m.AlternateViews.Add(av);

                sc.Host = "smtp.gmail.com";
                sc.Port = 587;
                sc.Credentials = new
                System.Net.NetworkCredential(from, password);
                sc.EnableSsl = true;
                sc.Send(m);

                EmailIsSent = true;

            }
            catch (Exception ex)
            {
                EmailIsSent = false;
            }

            return EmailIsSent;
        }

        // All the references of the current members are returned
        public List<Employee> getMemberRef()
        {

            IQueryable<Employee> Query =
                  from c in cecbContext.Employees

                  select c;

            List<Employee> SelectedEmpList = Query.ToList();

            return SelectedEmpList;

        }

        // Member name is passed as a parameter and the member reference of that name is returned
        public String getMemberRefByfName(String name)
        {
            var Ref = (from c in cecbContext.Employees
                       where c.EmpfName == name
                       select c).First();

            String memRef = Ref.EmpReference.ToString();


            return memRef;
        }

        // Return all the first names of the current members
        public List<Employee> getAllfNames()
        {
            IQueryable<Employee> Query =
            from c in cecbContext.Employees
            select c;

            List<Employee> AllfNames = Query.ToList();

            return AllfNames;
        }

        // Member reference is passed as a parameter and the member name is returned
        public List<Employee> getMemberNameByRef(String reference)
        {

            IQueryable<Employee> Query =
                  from c in cecbContext.Employees
                  where c.EmpReference == reference
                  select c;

            List<Employee> SelectedEmpList = Query.ToList();

            return SelectedEmpList;

        }

        // Member reference is passed as a parameter and the member email is returned
        public List<Employee_Email> getMemberEmailByRef(String reference)
        {

            IQueryable<Employee_Email> Query =
                  from c in cecbContext.Employee_Email
                  where c.Emp_Reference == reference
                  select c;

            List<Employee_Email> SelectedEmpEmailList = Query.ToList();

            return SelectedEmpEmailList;

        }

        // Size is passed as a parameter and a random password is generated in that size
        public string genarateRandomUsername(int Size)
        {
            Random ran = new Random();
            string input = "abcdefghijklmnopqrstuvwxyz";
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < Size; i++)
            {
                ch = input[ran.Next(0, input.Length)];
                builder.Append(ch);
            }
            return builder.ToString();
        }

        public string genarateRandomPassword(int Size)
        {
            Random ran = new Random();
            string input = "0123456789abcdefghijklmnopqrstuvwxyzWERTYUASDFGGHJJKZCXVBNM";
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < Size; i++)
            {
                ch = input[ran.Next(0, input.Length)];
                builder.Append(ch);
            }
            return builder.ToString();
        }




    }
}
